<?php

/**
 * Name: 登录控制器
 * User: Silent
 * Date: 2025-09-25
 * Time: 22:30:04
 */

namespace App\Http\Controllers\V1\Resources\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\WechatLoginRequest;
use App\Services\Auth\LoginService;
use Exception;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    protected $loginService;

    public function __construct(LoginService $loginService)
    {
        $this->loginService = $loginService;
    }

    public function login(WechatLoginRequest $request)
    {
        try {
            $inputs = $request->all();
            $result = $this->loginService->wxLogin($inputs['code']);
            if ($result['success']) {
                return $this->_success('登录成功', $result);
            }

            return $this->_error($result['message']);
        } catch (Exception $e) {

            return $this->_error($e->getMessage());
        }
    }

    public function setUserInfo(Request $request)
    {
        $inputs = $request->all();
        $result = $this->loginService->setUserInfo($inputs);
        if (!$result) {
            return $this->_error('设置失败');
        }

        return $this->_success('请求成功', $result);
    }

    public function getUserInfo(Request $request)
    {
        $id     = $request->input('id');
        $result = $this->loginService->getUserInfo($id);

        return $this->_success('请求成功', $result);
    }

    public function refreshToken(Request $request)
    {
        $openid = $request->input('openid');
        $result = $this->loginService->refreshToken($openid);

        return $this->_success('请求成功', $result);
    }

    /**
     *  验证公司部门信息（需要认证）
     *
     * @param Request $request
     * @return array
     */
    public function verify(Request $request)
    {
        try {
            $company    = $request->input('company');
            $department = $request->input('department');
            $wechatUser = $request->input('wechat_user'); // 从认证中间件获取微信用户
            
            if (empty($company)) {
                return $this->_error('公司名称不能为空');
            }
            if (empty($department)) {
                return $this->_error('部门名称不能为空');
            }

            // 验证公司和部门是否在配置的字典中
            $companies   = config('dictionary.company');
            $departments = config('dictionary.department');
            
            if (!in_array($company, $companies) || !in_array($department, $departments)) {
                return $this->_error('请提供正确的公司名称和部门信息，以便为内部人员使用的小程序进行设置。');
            }

            // 更新微信用户的公司和部门信息，如果有绑定员工则同步更新
            $result = $this->loginService->updateWechatUserCompanyInfo($wechatUser, $company, $department);
            
            if ($result['success']) {
                return $this->_success('验证成功，信息已更新', $result['data']);
            } else {
                return $this->_error($result['message']);
            }

        } catch (Exception $e) {
            return $this->_error('验证失败：' . $e->getMessage());
        }
    }
}
